package com.sgb.kjwl.viewmodel.shopping;

import com.sgb.kjwl.api.APIManager;
import com.sgb.jicai.entity.CentralizedMiningEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.view.entity.MenuEntity;
import com.swgk.core.util.PickSelectEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.repository.ShoppingCarRepertory;
import com.sgb.kjwl.view.ui.fragment.shop.ShopGroupBuyChildFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class ShopGroupBuyChildModel extends BaseViewModel {
    private ShoppingCarRepertory repertory;
    private ShopGroupBuyChildFragment fragment;
    private int page = 1;
    private Map map;

    public ShopGroupBuyChildModel(ShopGroupBuyChildFragment shopGroupBuyChildFragment,ShoppingCarRepertory repertory) {
        this.fragment = shopGroupBuyChildFragment;
        this.repertory = repertory;
    }

    //集采时间
    public void getDateTimeList() {
        APIManager.getInstance().getCentralizedMiningAPI().getDateTimeLists().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setTimeDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
            }
        });
    }

    //集采地
    public void getSecondAreaCodeList() {
        APIManager.getInstance().getCentralizedMiningAPI().getSecondAreaCodeList(repertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setcityAreaDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
            }
        });
    }

    //根据一级分类获取二级分类 type为一级类型(接口返回为三级，目前只用到第二级)
    public void getSecondTypeList(String type, int centralized) {
        APIManager.getInstance().getCentralizedMiningAPI().getSecondCategorys(type, String.valueOf(centralized)).enqueue(new Callback<BaseEntity<List<MenuEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<MenuEntity>>> call, Response<BaseEntity<List<MenuEntity>>> response) {
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setSecondTypeDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<MenuEntity>>> call, Throwable t) {

            }
        });
    }

    public void getMethodList() {
        List<PickSelectEntity> datas = new ArrayList<>();
        datas.add(new PickSelectEntity("", "全部"));
        datas.add(new PickSelectEntity("1", "即报即采"));
        datas.add(new PickSelectEntity("2", "汇集采购"));
        datas.add(new PickSelectEntity("3", "企业专项"));
        datas.add(new PickSelectEntity("4", "集采锁价"));
        fragment.setMethodDatas(datas);
    }



    public void getJiCaiList(String firstType, String secondTypeNo, String groupType, String provinceNo, String cityNo, String statusNo, String timeIntervals, int groupClass, String allSearchKey, String methodNo, String offerNo, boolean b) {
        map = new HashMap();
        page = b ? page + 1 : 1;
        map.put("limit", 10);
        map.put("current", page);
//        map.put("provinceNo", repertory.getAreaCode());//省编号
        map.put("provinceNo", provinceNo);//（省）
        map.put("cityNo", cityNo);//市级编号
//        map.put("districtNo", "");//区编号   --未使用
        map.put("endDayType", timeIntervals);//报价截止时间：1,0-5天，2，6-10天，3，11-20天，4,20-30天。5，31-50天，6，51-70天，7，71-90天
        if ("".equals(groupType)) {//集采大厅
            map.put("groupBuyType", methodNo);//集采方式  0  全部   1.即报即采  2.汇集采购  3.企业专项
        } else {
            map.put("groupBuyType", groupType);
            if ("3".equals(groupType)){
                map.put("groupBuyMode", "2"); //集采类型：1,需方集采，2企业专项
            }else if ("1".equals(groupType)){
                map.put("groupBuyMode", "1"); //集采类型：1,需方集采，2企业专项
            }
        }
        if (groupClass != 0) map.put("groupbuyClass", groupClass);//集采类型  商品、机械、设备…………
//        map.put("joinEndtime", "");//加盟截止时间   --未使用
        if ("".equals(groupType)) {//集采大厅
            if ("3".equals(methodNo)) {//企业专项筛选
                map.put("offerType", 2);//报价类型
            } else {
                map.put("offerType", offerNo);//报价类型
            }
        } else {
            map.put("offerType", offerNo);//报价类型
        }
//        map.put("order", "");//排序,,1，加盟数量，2，报价数量,3，加盟时间
        map.put("searchKey", allSearchKey);//搜索内容
        map.put("status", statusNo);//状态[1 加盟中 2 报价中 3集采完成
        map.put("typeNo", firstType);//集采类别
        if (null != secondTypeNo && !"".equals(secondTypeNo)) {
            map.put("typeNo", secondTypeNo);//最后一级类目
        }
//        map.put("createCompany", "");//发起企业
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getCentralizedMiningAPI().getJiCaiList(map).enqueue(new Callback<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>> call, Response<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    if (!b) {
                        fragment.showList(response.body().getData().getList());
                    } else {
                        fragment.lodMore(response.body().getData().getList(), response.body().getData().isLastPage());
                    }
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CentralizedMiningEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public String getAreaCode() {
        return repertory.getAreaCode();
    }

    public void getH5Url(String id) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getUrlSetting("collection").enqueue(new Callback<BaseEntity<H5ReEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<H5ReEntity>> call, Response<BaseEntity<H5ReEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {//type 0 首页 1 我的集采 2 我的报价
                    SharedPreferenceUtil.init(fragment.getActivity());
                    String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.resourceAPPToken, "");
                    String url = response.body().getData().getUrl() + "?id=" + id + "&type=0" + "&token=" + repertory.getToken();
//                    String url = "http://192.168.2.33:8067/collection" + "?id=" + id + "&type=0"+"&token="+ repertory.getToken();
                    fragment.showUrl(url);
                } else {
                    MToast.showToast(fragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<H5ReEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


    public void getProvinceData() {
        Map map = new HashMap();
        map.put("areaCode", repertory.getAreaCode());
//        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getLocationNew(map).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
//                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setProvinceDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
//                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getCityData(String province) {
        DialogHelper.showProgressDialog(fragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPickAPI().getAreaSecond(province).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setCityDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }


}
